package car

import (
	"carsrv/config"
	"carsrv/internal/model/dao"
	__ "carsrv/proto"
	"context"
	"encoding/json"
	"go.uber.org/zap"
	"strconv"
)

// 定义空接口
type CarService struct {
	__.UnimplementedCarServiceServer
}

// 实现方法
func (s *CarService) CarFind(ctx context.Context, req *__.CarFindRequest) (resp *__.CarFindResponse, err error) {
	var car dao.GvaCarTypes
	config.DB.Where("type=?", req.Type).Limit(1).Find(&car)
	if car.ID == 0 {
		zap.S().Error("没有该类型")
		return
	}
	marshal, _ := json.Marshal(car)
	key := "car" + strconv.Itoa(int(car.ID))
	config.Rdb.Set(key, marshal, 0)
	return &__.CarFindResponse{
		Id:   int32(car.ID),
		Type: car.Type,
		Img:  car.Img,
	}, nil
}
